
#ifndef __spthread_hpp__
#define __spthread_hpp__

// win32 thread

#include <winsock2.h>
#include <process.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef unsigned sp_thread_t;

typedef unsigned sp_thread_result_t;
#define SP_THREAD_CALL __stdcall
typedef sp_thread_result_t ( __stdcall * sp_thread_func_t )( void * args );

typedef HANDLE  sp_thread_mutex_t;
typedef HANDLE  sp_thread_cond_t;

//typedef DWORD   sp_thread_attr_t;
typedef struct tagsp_thread_attr {
	int stacksize;
	int detachstate;
} sp_thread_attr_t;

#define SP_THREAD_CREATE_DETACHED 1

//#ifndef sp_sleep
//#define sp_sleep(x) Sleep(1000*x)
//#endif

int sp_thread_mutex_init( HANDLE * mutex, void * attr );
int sp_thread_mutex_destroy( HANDLE * mutex );
int sp_thread_mutex_lock( HANDLE * mutex );
int sp_thread_mutex_unlock( HANDLE * mutex );

int sp_thread_cond_init( HANDLE * cond, void * attr );
int sp_thread_cond_destroy( HANDLE * cond );
int sp_thread_cond_wait( HANDLE * cond, HANDLE * mutex );
int sp_thread_cond_signal( HANDLE * cond );

int sp_thread_attr_init( sp_thread_attr_t * attr );
int sp_thread_attr_destroy( sp_thread_attr_t * attr );
int sp_thread_attr_setdetachstate( sp_thread_attr_t * attr, int detachstate );
int sp_thread_attr_setstacksize( sp_thread_attr_t * attr, int stacksize );

int sp_thread_create( unsigned * thread, sp_thread_attr_t * attr,
		sp_thread_func_t myfunc, void * args );
unsigned sp_thread_self();

#ifdef __cplusplus
}
#endif

#endif
